package zyy.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import zyy.util.Bus;
import zyy.util.BusinessException;

class FrmVipTime extends JDialog implements ActionListener {
  private static final long serialVersionUID = 658741659025403864L;
  private JPanel text = new JPanel();
  private JPanel btns = new JPanel();
  private JLabel label = new JLabel("VIP有效期至");
  private JTextField edt = new JTextField(20);
  private JButton commit = new JButton("提交");
  private JButton exit = new JButton("退出");

  private String fType;

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.commit) {
      Bus.VIPtime = this.edt.getText();
      try {
        if (fType.equals("管理员")) {
          Bus.userManager.setVIP(Bus.curVAdminUser.getUserID(), Bus.VIPtime);
          Bus.userManager.checkVIP(Bus.curVAdminUser.getUserID());
          Bus.fAdminUser.reloadVUserTable();
        } else {
          Bus.userManager.setVIP(Bus.currentUser.getUserID(), Bus.VIPtime);
          Bus.userManager.checkVIP(Bus.currentUser.getUserID());
          Bus.userManager.loadUserInfo(Bus.fUserInfo);
        }
      } catch (BusinessException ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
      }
      this.setVisible(false);
    } else if (e.getSource() == this.exit) {
      this.setVisible(false);
    }
  }

  public FrmVipTime(String type) {
    // 基础 JDialog 设置
    {
      this.setTitle("外卖助手 " + type + " 用户VIP有效期设置");
      this.setSize(295, 145);
      this.setLocationRelativeTo(null);
      this.setVisible(true);
      this.fType = type;
    }
    // this.getContentPane() 一级布局
    {
      this.getContentPane().setLayout(new BorderLayout());
      this.getContentPane().add(this.text, BorderLayout.CENTER);
      this.getContentPane().add(this.btns, BorderLayout.SOUTH);
    }
    // text 相关配置
    {
      this.text.setLayout(new FlowLayout(FlowLayout.LEFT));
      this.text.add(this.label);
      this.text.add(this.edt);
    }
    // btns 相关配置
    {
      this.btns.setLayout(new FlowLayout(FlowLayout.RIGHT));
      this.btns.add(this.commit);
      this.btns.add(this.exit);
      this.commit.addActionListener(this);
      this.exit.addActionListener(this);
      Bus.jButtonZYYStyle(this.commit);
      Bus.jButtonZYYStyle(this.exit);
    }
  }

}